package com.rong.juc_test.juc_base.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 集齐7颗龙珠召唤神龙
 */
public class CyclicBarrierDemo {
    private static final int NUMBER = 7;
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(NUMBER,() -> {
            System.out.println("集齐7颗龙珠召唤神龙");
        });
        // 集齐7颗龙珠的过程
        for (int i = 1; i <= NUMBER; i++) {
            new Thread(() ->{
                try {
                    System.out.println(Thread.currentThread().getName() + "星龙珠");
                    cyclicBarrier.await(); // 没有达到7颗龙珠，等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();

        }

    }
}
